package marco.android.CouchControl;

import android.view.animation.Interpolator;
import android.view.animation.OvershootInterpolator;

public class FlingHandler
{
	private Interpolator mInterpolator;
	private long mStartTime;
	private long mEndTime;
	private float mTotalDx;
	private float mTotalDy;
	private boolean doFling = false;
	
	public boolean onFling(float dx, float dy, long durationMilliSecs)
	{
		mInterpolator = new OvershootInterpolator();
		mStartTime = System.currentTimeMillis();
		mEndTime = mStartTime + durationMilliSecs;
		mTotalDx = dx;
		mTotalDy = dy;
		doFling = true;
		return(doFling);
	}
	
	public boolean DoFling(float [] pos)
	{
		if(doFling == true && pos.length > 1)
		{
			long curTime = System.currentTimeMillis();
			float pctTime = (float) (curTime - mStartTime) / (float)(mEndTime - mStartTime);
			if(pctTime < 1.f)
			{
				float percentDistance = mInterpolator.getInterpolation(pctTime);
				float curDx = percentDistance * mTotalDx;
				float curDy = percentDistance * mTotalDy;
				pos[0] = curDx;
				pos[1] = curDy;
			}
			else
			{
				doFling = false;
			}
		}
		return(doFling);
	}
}
